package com.yc.encrypt;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * Created with IntelliJ IDEA.
 * description:
 * author: YC
 * date: 2020/10/28
 * time: 21:04
 */
public class TestCipher {
    /**
     * 加密
     * @param content   被加密字符串
     * @param slatKet   加密的盐
     * @param vectorKey 加密向量
     * @return
     */
    public String encry(String content, String slatKet, String vectorKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKey secretKey = new SecretKeySpec(slatKet.getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte [] encryed = cipher.doFinal(content.getBytes());
//        return new String(encryed);
        return Base64.getEncoder().encodeToString(encryed);
    }

    public void test2() {
        AES AES = SecureUtil.aes("i42pg87bsdr8bgv9p34bkjbkljzaOQWI".getBytes());
        System.out.println(AES.encryptBase64("1"));
    }

    public static void main(String[] args) throws Exception {
        TestCipher tc = new TestCipher();
//        System.out.println(tc.encry("1", "1234567890123456", "1234567890123456"));
        tc.test2();
    }
}
